Visualizing Surveys in R by Valtonen Teppo;

Visualizing Surveys in R by Valtonen Teppo;

Author:Valtonen, Teppo;
Language: eng
Format: epub
Publisher: CRC Press LLC
Published: 2023-11-03T00:00:00+00:00


8.2.1 Parse NA values in numeric variables

Since I have read the data as text, I can also use the parse_integer() and parse_numeric() functions described in Section 6.1 and 6.2, respectively:

df %>% select( LSATIS11R_A) %>% mutate( lifesat11 = parse_integer( LSATIS11R_A, na = c( '97', '98', '99' ) ) ) %>% head()

## # A tibble: 6 x 2 ## LSATIS11R_A lifesat11 ## <chr> <int> ## 1 8 8 ## 2 8 8 ## 3 9 9 ## 4 8 8 ## 5 8 8 ## 6 10 10

8.2.2 NA values in categorical variables

If I have categorical variables, probably the easiest way is to use the function factor() and define non-NA values with the levels argument:

df %>% select( PAIFRQ3M_A) %>% mutate( pain = factor( PAIFRQ3M_A, levels = c( 1, 2, 3, 4 ) ) ) %>% head()

## # A tibble: 6 x 2 ## PAIFRQ3M_A pain ## <chr> <fct> ## 1 2 2 ## 2 4 4 ## 3 2 2 ## 4 4 4 ## 5 8 <NA> ## 6 8 <NA>

Like I described in Section 6.3, I can also use the function parse_factor() but if I don't want to get warnings, I have to define both NA and non-NA values explicitly:

df %>% select( PAIFRQ3M_A) %>% mutate( pain = parse_factor( PAIFRQ3M_A, levels = c( '1', '2', '3', '4' ), na = c( '7', '8', '9' ) ) ) %>% head()

## # A tibble: 6 x 2 ## PAIFRQ3M_A pain ## <chr> <fct> ## 1 2 2 ## 2 4 4 ## 3 2 2 ## 4 4 4 ## 5 8 <NA> ## 6 8 <NA>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.